package au.edu.uq.itee.engg2800.team19;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BasicGui extends JFrame implements ActionListener{

	private static final long serialVersionUID = -6150469731699254821L;

	// Initialize all swing objects.

	// Menu
    private JMenuBar mb = new JMenuBar(); // Menubar
    private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar
    private JMenuItem mnuConnectionProps=new JMenuItem("Connect");
    private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item
    private JMenu mnuHelp = new JMenu("Help"); // Help Menu entry
    private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry

    //The Frequency Holder
	private FrequenciesReceived freqs=new FrequenciesReceived();

	//The Serial Connection
	private RS232Conn serialConn=new RS232Conn(freqs);

	//Panels for GUI
	private FrequencyPlot freqPlot;
	private ToggleStartStop runStop;

	/** Constructor for the GUI */
    public BasicGui(){
        // Set menubar
    	super("Jterm");
        this.setJMenuBar(mb);

        //setup views
        runStop= new ToggleStartStop(serialConn,freqs);
        freqPlot =new FrequencyPlot(freqs);

        //Build Menus
        mnuFile.add(mnuConnectionProps);
        mnuFile.add(mnuItemQuit);  // Create Quit line

        mnuHelp.add(mnuItemAbout); // Create About line
        mb.add(mnuFile);        // Add Menu items to form
        mb.add(mnuHelp);

        // Setup Main Frame
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(freqPlot,BorderLayout.NORTH);
        this.getContentPane().add(runStop,BorderLayout.CENTER);
		pack();
        // Allows the Swing App to be closed
        this.addWindowListener(new ListenCloseWdw());

        //Add Menu listener
        mnuConnectionProps.addActionListener(this);
        mnuItemQuit.addActionListener(new ListenMenuQuit());
        mnuItemAbout.addActionListener(this);
        this.setLocation(300, 0);
    }
    public class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    }


    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    }

    public void launchFrame(){
        // Display Frame
        this.setPreferredSize(new Dimension(1000,500));
    	this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack(); //Adjusts panel to components for display
        this.setLocationRelativeTo(null);
        this.setVisible(true);


    }

	@Override
	public void actionPerformed(ActionEvent evt) {

		Object source = evt.getSource();

		if (source == mnuConnectionProps) {

	    	try {
				serialConn.connect();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("oops");
			}
	    }

		if (source == mnuItemAbout) {
	    	JDialog f = new SimpleAboutDialog(this);
	    	f.setVisible(true);
	    }
	}
}
